/*
 * VHDispatcher.java
 * Created on 28 mai 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.mvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Serves as registry for all controllers and dispatches application level events.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHDispatcher
{
  private static VHDispatcher d_instance;
  private List<VHController>  d_listControllers;

  /** Private contructor to avoid instantiation. */
  private VHDispatcher()
  {}

  /**
   * Adds a new controller to this dispatcher. Registered controllers are informed about
   * application level events that are fired using the {@link #dispatch(VHEvent)} method.
   *
   * @param controller the controller to be added
   */
  public void addController( VHController controller )
  {
    if ( d_listControllers == null )
      d_listControllers = new ArrayList<VHController>();

    d_listControllers.add( controller );
  }

  /**
   * Dispatches the given event to all registered controllers.
   *
   * @param event the event to be dispatched
   */
  public void dispatch( VHEvent event )
  {
    if ( d_listControllers == null )
      return;

    for ( VHController c : d_listControllers )
      c.handleEvent( event );
  }

  /**
   * Returns the application event dispatcher. Only one dispatcher per application is
   * available.
   */
  public static VHDispatcher get()
  {
    if ( d_instance == null )
      d_instance = new VHDispatcher();

    return d_instance;
  }
}
